/*
ont-space - The ontology-based resource metadata repository
Copyright (c) 2006-2008, Information Eng. Research Unit - Univ. of Alcalá
http://www.cc.uah.es/ie
This library is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 2.1 of the License, or (at your option)
any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License along
with this library; if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
package org.ontspace.util.translator.sld2owl.owl;

import org.w3c.dom.DOMException;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.hp.hpl.jena.ontology.*;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.datatypes.RDFDatatype;
import com.hp.hpl.jena.datatypes.TypeMapper;

import java.util.ArrayList;

/**
 * This class translate the support activity element
 *
 */
public class SupportActivitiesTranslator {

    lastInstanceSLDTranslator lastInst;

    /**
     * Creates a new instance of SupportActivitiesTranslator    
     * @param parent The activity class
     * @param node The support activity node of the Dom structure
     * @param ont The ontology
     * @param instLD The learning design instance
     * @param nsName The namespace
     */
    public SupportActivitiesTranslator(OntClass parent, Node node, OntModel ont,
            Individual instLD, String nsName, ArrayList referencesEnvironment) {

        OntClass exeEnt =
                ont.getOntClass(nsName + "Execution-Entity");

        OntClass activity =
                ont.getOntClass(nsName + "Activity");
        DatatypeProperty identifier =
                ont.getDatatypeProperty(nsName + "identifier");
        DatatypeProperty title =
                ont.getDatatypeProperty(nsName + "title");
        ObjectProperty environmentRef =
                ont.getObjectProperty(nsName + "environment-ref");
        DatatypeProperty isVisible =
                ont.getDatatypeProperty(nsName + "isvisible");
        DatatypeProperty parameters =
                ont.getDatatypeProperty(nsName + "parameters");
        ObjectProperty roleRef =
                ont.getObjectProperty(nsName + "role-ref");


        lastInst =
                new lastInstanceSLDTranslator();
        String instance =
                "support_activity_instance_" +
                (String.valueOf(lastInst.lastInstance(parent)));
        String leTitle =
                null;
        NodeList learActiChildren =
                node.getChildNodes();
        if (learActiChildren != null) {
            for (int i =
                    0; i < learActiChildren.getLength(); i++) {

                Individual instLe =
                        ont.createIndividual(nsName + instance, parent);
                OntClass ld =
                        ont.getOntClass(nsName + "Learning-Design");

                ObjectProperty exeEntRef =
                        ont.getObjectProperty(nsName + "execution-entity-ref");
                instLD.setPropertyValue(exeEntRef, instLe);


                if (learActiChildren.item(i).getNodeName().
                        compareTo("imsld:title") == 0) {
                    leTitle =
                            learActiChildren.item(i).getTextContent();
                    try {
                        RDFNode titleValue =
                                ont.createTypedLiteral(leTitle);
                        instLe.setPropertyValue(title, titleValue);
                        NamedNodeMap attrsItem =
                                node.getAttributes();
                        for (int x =
                                0; x < attrsItem.getLength(); x++) {
                            Node attr =
                                    attrsItem.item(x);
                            if (attr.getNodeName().trim().
                                    compareTo("identifier") == 0) {
                                TypeMapper tm =
                                        TypeMapper.getInstance();
                                RDFDatatype overType =
                                        tm.getSafeTypeByName("http://www.w3.org/2001/XMLSchema#ID");
                                RDFNode value =
                                        ont.createTypedLiteral(attr.getNodeValue().
                                        trim(), overType);
                                instLe.setPropertyValue(identifier, value);
                            }

                            if (attr.getNodeName().trim().
                                    compareTo("isVisible") == 0) {
                                RDFNode visibleValue;
                                if (attr.getNodeValue().trim().compareTo("true") ==
                                        0) {
                                    visibleValue =
                                            ont.createTypedLiteral(true);
                                } else {
                                    visibleValue =
                                            ont.createTypedLiteral(false);
                                }

                                instLe.setPropertyValue(isVisible, visibleValue);
                            }


                            if (attr.getNodeName().trim().
                                    compareTo("parameters") == 0) {
                                RDFNode parametersValue =
                                        ont.createTypedLiteral(attr.getNodeValue().
                                        trim());
                                instLe.setPropertyValue(parameters,
                                        parametersValue);
                            }


                        }//for

                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
                if (learActiChildren.item(i).getNodeName().compareTo("imsld:role-ref") ==
                        0) {
                    NamedNodeMap attrsItem =
                            learActiChildren.item(i).getAttributes();
                    for (int x =
                            0; x < attrsItem.getLength(); x++) {
                        Node attr =
                                attrsItem.item(x);
                        if (attr.getNodeName().trim().compareTo("ref") == 0) {
                            try {

                                OntClass role =
                                        ont.getOntClass(nsName + "Role");
                                TypeMapper tm =
                                        TypeMapper.getInstance();
                                RDFDatatype overType =
                                        tm.getSafeTypeByName("http://www.w3.org/2001/XMLSchema#ID");
                                RDFNode identifierValue =
                                        ont.createTypedLiteral(attr.getNodeValue().
                                        trim(), overType);
                                searchInstance searchInst =
                                        new searchInstance();
                                Individual roleRefInst =
                                        searchInst.search(role, identifier,
                                        identifierValue);
                                instLe.setPropertyValue(roleRef, roleRefInst);


                            } catch (DOMException ex) {
                                ex.printStackTrace();
                            }
                        }
                    }


                }
                if (learActiChildren.item(i).getNodeName().compareTo("imsld:environment-ref") ==
                        0) {
                    NamedNodeMap attrsItem =
                            learActiChildren.item(i).getAttributes();
                    for (int x =
                            0; x < attrsItem.getLength(); x++) {
                        Node attr =
                                attrsItem.item(x);
                        if (attr.getNodeName().trim().compareTo("ref") == 0) {
                            try {

                                refTranslator reference =
                                        new refTranslator();
                                reference.inst =
                                        instLe;
                                reference.instRef =
                                        attr.getNodeValue().trim();
                                referencesEnvironment.add(reference);

                            } catch (DOMException ex) {
                                ex.printStackTrace();
                            }
                        }
                    }


                }
                if (learActiChildren.item(i).getNodeName().compareTo(
                        "imsld:activity-description") == 0) {
                    ActivityDescriptionTranslator dld =
                            new ActivityDescriptionTranslator(learActiChildren.item(i),
                            ont, instLe, nsName);
                }
                if (learActiChildren.item(i).getNodeName().compareTo(
                        "imsld:complete-activity") == 0) {
                    CompleteActivityTranslator dcac =
                            new CompleteActivityTranslator(learActiChildren.item(i),
                            ont, instLe, nsName);
                }
                if (learActiChildren.item(i).getNodeName().
                        compareTo("imsld:on-completion") == 0) {
                    NodeList feed =
                            learActiChildren.item(i).getChildNodes();
                    for (int f =
                            0; f < feed.getLength(); f++) {
                        if (feed.item(f).getNodeName().
                                compareTo("imsld:feedback-description") == 0) {
                            FeedbackDescriptionTranslator dfd =
                                    new FeedbackDescriptionTranslator(feed.item(i),
                                    ont, instLe, parent, nsName);
                        }
                    }
                }

            }
        }


    }
}
